package com.jeesite.modules.dataview.specialWork.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.dataview.chaierweijin.entity.DcSupImportData;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.entity.DcSupTaskActionDto;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dataview.specialWork.entity.DcSupSpecialWork;
import com.jeesite.modules.dataview.specialWork.service.DcSupSpecialWorkService;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 专项工作-事项清单Controller
 * @author dbl
 * @version 2022-09-13
 */
@Controller
@RequestMapping(value = "${adminPath}/specialWork/dcSupSpecialWork")
public class DcSupSpecialWorkController extends BaseController {

	@Autowired
	private DcSupSpecialWorkService dcSupSpecialWorkService;
	@Autowired
	private DcSupTaskInfoDictService dcSupTaskInfoDictService;
	@Autowired
	private UserService userService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	@RequestMapping(value = {"specialWorkOne", ""})
	public String pageOne(DcSupSpecialWork dcSupSpecialWork, Model model) {
		Integer allNums = dcSupSpecialWorkService.queryAllNums(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode(),UserUtils.getUser().getUserCode());
		model.addAttribute("dcSupImportData", dcSupSpecialWork);
		model.addAttribute("allNums", allNums);
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);
		model.addAttribute("userList", list);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		return "dataview/specialWork/specialWorkOne";
	}


	@RequestMapping(value = {"specialWorkTwo", ""})
	public String pageTwo(DcSupSpecialWork dcSupSpecialWork, Model model) {
		model.addAttribute("dcSupImportData", dcSupSpecialWork);
		return "dataview/specialWork/specialWorkTwo";
	}

	@RequestMapping(value = {"specialWorkThree", ""})
	public String pageThree(DcSupSpecialWork dcSupSpecialWork, Model model) {
		model.addAttribute("dcSupImportData", dcSupSpecialWork);
		return "dataview/specialWork/specialWorkThree";
	}
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupSpecialWork get(String id, boolean isNewRecord) {
		return dcSupSpecialWorkService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("specialWork:dcSupSpecialWork:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcSupSpecialWork dcSupSpecialWork, Model model) {
		dcSupSpecialWork.setImportTime(new Date());
		model.addAttribute("dcSupSpecialWork", dcSupSpecialWork);
		return "dataview/specialWork/dcSupSpecialWorkList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("specialWork:dcSupSpecialWork:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupSpecialWork> listData(DcSupSpecialWork dcSupSpecialWork, HttpServletRequest request, HttpServletResponse response) {
		dcSupSpecialWork.setPage(new Page<>(request, response));
		if (dcSupSpecialWork.getImportTime()==null){
//			dcSupImportData.setC28Str(LocalDate.now().toString());
		}else{
			dcSupSpecialWork.setImportTimeStr(DateUtils.formatDate(dcSupSpecialWork.getImportTime(),"yyyy-MM-dd"));
		}
		Page<DcSupSpecialWork> page = dcSupSpecialWorkService.findPage(dcSupSpecialWork);
		return page;
	}


	/**
	 * 保存数据
	 */
	@RequiresPermissions("specialWork:dcSupSpecialWork:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupSpecialWork dcSupSpecialWork) {
		dcSupSpecialWorkService.save(dcSupSpecialWork);
		return renderResult(Global.TRUE, text("保存专项工作-事项清单成功！"));
	}

	/**
	 * 导入
	 * @param file
	 * @return
	 */
	@PostMapping(value = "importData")
	@ResponseBody
	public String importData(MultipartFile file, Date importTime) {
		try {
			String message = dcSupSpecialWorkService.importData(file, importTime);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}

	@RequestMapping(value = "specialWorkOneList")
	public String specialWorkOneList(DcSupSpecialWork dcSupSpecialWork, Model model) {
		model.addAttribute("dcSupImportData", dcSupSpecialWork);
		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);
		model.addAttribute("userList", list);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		return "dataview/specialWork/specialWorkOneList";
	}

	@RequestMapping(value = "specialWorkOnePage")
	public String specialWorkOnePage(DcSupSpecialWork dcSupSpecialWork, Model model) {
		model.addAttribute("dcSupImportData", dcSupSpecialWork);
		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);
		model.addAttribute("userList", list);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupSpecialWork", dcSupSpecialWork);
		return "dataview/specialWork/specialWorkOnePage";
	}

	/**
	 * 保存数据
	 */
	@PostMapping(value = "queryConditionNums")
	@ResponseBody
	public String queryConditionNums(DcSupTaskActionDto dcSupTaskActionDto) {
		dcSupTaskActionDto.setRemarks3(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		Integer nums  = dcSupSpecialWorkService.queryConditionNums(dcSupTaskActionDto);
		return renderResult(Global.TRUE, text("全部任务数"),nums);
	}
}